package com.flashcash.solu.view.me;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.flashcash.solu.R;
import com.flashcash.solu.app.base.BaseActivity;
import com.flashcash.solu.common.FieldParams;
import com.flashcash.solu.utils.TimeFormat;
import com.flashcash.solu.view.MsgBoxActivity;
import com.flashcash.solu.view.me.presenter.MyInvitedInfoActActPreImpl;
import com.flashcash.solu.view.me.presenter.MyInvitedInfoActPresenter;
import com.flashcash.solu.widget.xleotoast.XLeoToast;
import com.flashcash.kotlin.data.InviteResult;
import com.flashcash.kotlin.data.InviteePersonBean;
import com.x.leo.listexpend.AutoRollListView;
import com.x.leo.listexpend.CycleAdapter;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * @作者:My
 * @创建日期: 2017/3/23 15:23
 * @描述:${TODO}
 * @更新者:${Author}$
 * @更新时间:${Date}$
 * @更新描述:${TODO}
 */

public class MyInvitedInfoActivity extends BaseActivity<MyInvitedInfoActPresenter> implements MyInvitedInfoActView, View.OnClickListener {

    @BindView(R.id.id_imagebutton_back)
    ImageButton idImagebuttonBack;
    @BindView(R.id.id_textview_title)
    TextView idTextviewTitle;
    @BindView(R.id.id_imagebutton_info_list)
    ImageButton idImagebuttonInfoList;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.btn_copy)
    Button btnCopy;
    @BindView(R.id.tv_invited)
    TextView tvInvited;
    @BindView(R.id.tv_applied)
    TextView tvApplied;
    @BindView(R.id.list_content)
    AutoRollListView listContent;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_invite;
    }

    @Override
    protected void init() {
        idImagebuttonBack.setOnClickListener(this);
        idImagebuttonInfoList.setOnClickListener(this);
        idTextviewTitle.setText(R.string.undang_teman);
        mPresenter.obtainCode();
        btnCopy.setOnClickListener(this);
        String url = getIntent().getStringExtra(FieldParams.ACTIVITY_DETAIL_IMAGE_URL);
//        if (url != null) {
//            Glide.with(this).load(url).diskCacheStrategy(DiskCacheStrategy.NONE).listener(new RequestListener<String, GlideDrawable>() {
//                @Override
//                public boolean onException(Exception e, String s, Target<GlideDrawable> target, boolean b) {
//                    return false;
//                }
//
//                @Override
//                public boolean onResourceReady(GlideDrawable glideDrawable, String s, Target<GlideDrawable> target, boolean b, boolean b1) {
//                    try {
//                        if (glideDrawable instanceof GlideBitmapDrawable && target instanceof GlideDrawableImageViewTarget) {
//                            int intrinsicWidth = glideDrawable.getIntrinsicWidth();
//                            int intrinsicHeight = glideDrawable.getIntrinsicHeight();
//                            double rate = (intrinsicHeight + 0.5) / intrinsicWidth;
//                            ImageView view = ((GlideDrawableImageViewTarget) target).getView();
//                            int measuredWidth = view.getMeasuredWidth();
//                            if (measuredWidth == 0) {
//                                view.measure(0, 0);
//                                measuredWidth = view.getMeasuredWidth();
//                            }
//                            int resultHeight = (int) (measuredWidth * rate + 0.5);
//                            if (resultHeight > 0) {
//                                ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
//                                layoutParams.height = resultHeight;
//                                layoutParams.width = measuredWidth;
//                                view.setLayoutParams(layoutParams);
//                            }
//                        }
//                    }catch (Throwable t){
//                    }
//                    return false;
//                }
//            }).into((ImageView) findViewById(R.id.iv_bg));
//        }
    }

    @Override
    protected MyInvitedInfoActPresenter initPresenterImpl() {
        return new MyInvitedInfoActActPreImpl();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.id_imagebutton_back:
                finish();
                break;
            case R.id.id_imagebutton_info_list:
                startActivity(new Intent(this, MsgBoxActivity.class));
                break;
            case R.id.btn_copy:
                addToClipBoard(tvCode.getText());
                break;
        }
    }

    private void addToClipBoard(CharSequence s) {
        ClipboardManager clipBoard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clipData = ClipData.newPlainText("text/plain", s);
        if (clipBoard != null) {
            clipBoard.setPrimaryClip(clipData);
            XLeoToast.showMessage(R.string.show_has_copyed_to_clip_board);
        }
    }

    @Override
    public void onCodeObtain(String s) {
        tvCode.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        tvCode.getPaint().setAntiAlias(true);
        tvCode.setText(s);
    }

    @Override
    public void onInviteResultObtain(InviteResult inviteResult) {
        tvCode.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        tvCode.getPaint().setAntiAlias(true);
        tvCode.setText(inviteResult.getCode());
        //tvApplied.setText(String.format(getString(R.string.invite_info_sum),inviteResult.getBean().getInviteeCount()));
        tvInvited.setText(String.format(getString(R.string.invite_info_sum),inviteResult.getBean().getInviteeCount()));
        initList(inviteResult.getList());
    }

    private void initList(final ArrayList<InviteePersonBean> list) {
        if (list == null) {
            return;
        }
        listContent.setAdapter(new CycleAdapter() {
            @Override
            public int getRealCount() {
                return list.size();
            }

            @NotNull
            @Override
            public InviteePersonBean getRealItem(int position) {
                return list.get(position);
            }

            @NotNull
            @Override
            public View getRealView(int i, View convertView, ViewGroup parent) {
                Holder holder = getHolder(convertView);
                holder.inject(getRealItem(i));
                return holder.itemView;
            }

            private Holder getHolder(View convertView) {
                if (convertView == null) {
                    convertView = View.inflate(MyInvitedInfoActivity.this.getApplicationContext(),R.layout.item_inviteinfo_list,null);
                    Holder holder = new Holder(convertView);
                    convertView.setTag(holder);
                    return holder;
                }else{
                    return (Holder) convertView.getTag();
                }
            }
            class Holder{
                View itemView;
                TextView leftView;
                TextView middleView;
                TextView rightView;
                public Holder(View convertView) {
                   itemView = convertView;
                    leftView =  convertView.findViewById(R.id.left_text);
                    middleView = convertView.findViewById(R.id.middle_text);
                    rightView =  convertView.findViewById(R.id.right_text);
                }

                void inject(InviteePersonBean item) {
                    leftView.setText(item.getMobile());
                    middleView.setText(item.getRealName());
                    rightView.setText(TimeFormat.convertTimeDay(item.getRegisterTime()));
                }
            }
        });
    }


}
